package o2o.web.actions.sys;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.JYPageUtils;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.sys.SysConfigDepositMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.config.redis.RedisManager;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.sys.SysConfigDeposit;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.sys.SysConfigDepositVO;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static o2o.config.redis.RedisKeyConstant.SYS_CONFIG_DEPOSIT_TYPE_MAP_PREFIX;

// 押金设置

@Controller
public class SysConfigDepositAction {

    // 获取所有押金设置
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/config/deposit/list")
    @ResponseBody
    public String sysConfigPayList(String token, Integer type, Integer page, Integer size) {
        //登录验证
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        //权限验证
        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有访问权限");
        }

        if (null == page) {
            page = 1;
        }
        if (null == size) {
            size = 20;
        }
        List<SysConfigDeposit> list;
        if (null != type) {
            list = SysConfigDepositMemory.getInstance().listOfType(type);
        } else {
            list = SysConfigDepositMemory.getInstance().list();
        }

        if (ValidateUtil.isAbsList(list)) {
            JYPageUtils.PageList pageList = JYPageUtils.getList(list, page, size);
            if (null != pageList && ValidateUtil.isAbsList(pageList.list)) {
                List<SysConfigDepositVO> voList = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    if (null == list.get(i)) {
                        continue;
                    }
                    SysConfigDepositVO vo = new SysConfigDepositVO();
                    try {
                        ObjectUtils.copyPropertiesExclude(list.get(i), vo);
                    } catch (Exception e) {
                        e.printStackTrace();
                        continue;
                    }
                    voList.add(vo);
                }
                return AjaxResult.successResult(voList.size(), voList);
            }

        }
        return AjaxResult.successResult(0, 0, null);
    }

    @RequestMapping(value = "/admin/sys/config/deposit/get", method = RequestMethod.GET)
    @ResponseBody
    public String getSysConfigPay(@RequestParam("listType") int listType) {
        Double res = SysConfigDepositMemory.getInstance().getByType(listType);
        System.out.println(res);
        return AjaxResult.successResult(res);

    }

    // 修改押金设置
    @RequestMapping(method = RequestMethod.POST, value = "/admin/sys/config/deposit/update")
    @ResponseBody
    public String updateSysConfigPay(String token, SysConfigDeposit info) {
        //登录验证
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        //权限验证
        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有访问权限");
        }
        if (!ValidateUtil.isID(info.getId())) {
            return AjaxResult.errorResult("id不能为空");
        }
        SysConfigDeposit configDeposit = SysConfigDepositMemory.getInstance().getById(info.getId());

        //理论上不存在此情况
        if (null == configDeposit) {
            return AjaxResult.errorResult("信息不存在，请查证后再试");
        }
        boolean isUpdatePay = false;
        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.SYS_CONFIG_DEPOSIT).append(" set ");

        if (null != info.getMoney()) {
            isUpdatePay = true;
            sb.append(" money = ").append(info.getMoney()).append(",");
        }

        if (null != info.getName()) {
            isUpdatePay = true;
            sb.append(" name = '").append(info.getName()).append("',");
        }
        if (null != info.getNote()) {
            isUpdatePay = true;
            sb.append(" note = '").append(info.getNote()).append("',");
        }

        sb.append(" updateUser = '").append(user.getUsername()).append("'");
        sb.append(" where id =").append(info.getId()).append(" ");
        if (!isUpdatePay) {
            return AjaxResult.successResult();
        } else {
            try {
                UserDaoSql.getInstance().executeBySql(sb.toString());
                RedisManager.hdel(SYS_CONFIG_DEPOSIT_TYPE_MAP_PREFIX, info.getId().toString());
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("更新失败，请稍后再试.");
            }
        }

        return AjaxResult.successResult();
    }


}
